new Vue({
    el:'#categoryManage',
    data:{
        pageInfo:'',
        infoFlag:false,
        infoText:'',
        cate_type:0,
        first_type:[],
        second_type:[],
        firstType:0,
        secondType:0,
        cateName:"(必填)",
        cate_name:'',
        addFlag:false,
        cateRadio:'',
        // cateType:'',
        styleObj:{
            backgroundColor:"#ff4e00",
            border:"none",
            height:"23px",
            width:"23px",
            color:"white"
        },
        no_bgc:{
            height:"22px",
            width:"22px",
        },
        category:{
            id:'',
            name:'',
            type:'',
            parentId:'',
            p_parentId:'',
            parent_name:'',
            p_parent_name:'',
            iconClass:''
        },updateCate:false
    },
    methods:{
        getCategoryPage(pageNum,pageSize)
        {
            axios.get('/api/getCategoryPage',
                {
                    params:{
                        pageNum,pageSize,
                    }
                }).then(rs=>{
                this.pageInfo=rs.data.pageInfo
                console.log(rs.data)
            })
        },
        opera(sign)
        {
            let pageNum=1
            switch(sign)
            {
                case "pre":
                    pageNum=this.pageInfo.hasPreviousPage?this.pageInfo.prePage:1
                    break;
                case "next":
                    pageNum=this.pageInfo.hasNextPage?this.pageInfo.nextPage:this.pageInfo.pages
                    break;
                case "last":
                    pageNum=this.pageInfo.pages
                    break;
            }
            this.getCategoryPage(pageNum,5)
        },
        getCategoryList()
        {
            this.first_type=[]
            this.second_type=[]
            console.log("类别"+this.cate_type)
            if(this.cate_type>=2)
            {
                axios.get('/api/getCategoryList',
                    {
                        params:{
                            type:this.cate_type
                        }
                    }).then(rs=>{
                    rs.data.cateList.forEach(cate=>{
                        if(cate.type==1) {
                            this.first_type.unshift(cate)
                        }
                        // }else{
                        //     this.second_type.unshift(cate)
                        // }
                    })
                    console.log(this.first_type)
                    // console.log(this.second_type)
                })
            }else{
                this.firstType=0
            }
        },
        getCategoryListInThis()
        {
            this.second_type=[]
            console.log(this.cate_type)
            if(this.cate_type===3 && this.firstType>0)
            {
                axios.get('/api/getCategoryListInThis',
                    {
                        params:{
                            id:this.firstType
                        }
                    }).then(rs=>{
                    rs.data.cateList.forEach(cate=>{
                        this.second_type.unshift(cate)
                    })
                    console.log(this.second_type)
                    console.log('first'+this.firstType)
                    console.log('second'+this.secondType)
                })
            }else{
                this.second_type=[]
            }
        },
        checkCateName()
        {
            let parentId=0
            if(this.cate_type===2)
            {
                parentId=this.firstType
            }else if(this.cate_type===3)
            {
                parentId=this.secondType
            }
            if(this.cate_type!=0)
            {
                axios.get('/api/checkCateName',
                    {
                        params:{
                            name:this.cate_name,
                            type:this.cate_type,
                            parentId
                        }
                    }
                ).then(rs=>{
                    if(rs.data.isExist==="yes")
                    {
                        this.cateName="分类已存在"
                        this.$refs.addCategory.disabled=true
                    }
                    if(rs.data.isExist==="no")
                    {
                        this.cateName="分类可用"
                        this.$refs.addCategory.disabled=false
                    }
                    if(rs.data.isExist==="null")
                    {
                        this.cateName="分类不能为空"
                        this.$refs.addCategory.disabled=true
                    }

                })
            }

        },
        addCategory()
        {
            let parentId=0
            if(this.cate_type===2)
            {
                parentId=this.firstType
            }else if(this.cate_type===3)
            {
                parentId=this.secondType
            }
            if(this.cate_type===0)
            {
                this.infoFlag=true
                this.infoText="请选择级别"
                return
            }
            axios.get('/api/addCategory',
                {
                    params:{
                        name:this.cate_name,
                        type:this.cate_type,
                        parentId
                    }
                }).then(rs=>{
                    this.infoFlag=true
                    if(rs.data.isAdd==="yes")
                    {
                        this.infoText="添加成功"
                    }else{
                        this.infoText="添加失败"
                    }

            })
        },

        //修改回显
        getUpdateCategory(index)
        {
            let cateType=this.$refs['cateType'+index][0]._value
            this.updateCate=true
            axios.get('/api/getUpdateCategory',{
                params:{
                    id:this.cateRadio,
                    cateType
                }
            }).then(rs=>{
                this.category=rs.data.category
                this.cate_type=rs.data.category.type
                if(this.cate_type===3)
                {
                    this.firstType=rs.data.category.p_parentId
                    console.log('sdsdsds'+this.firstType)
                    this.secondType=rs.data.category.parentId
                    console.log('sdsddfdfdsds'+this.secondType)
                }else if(this.cate_type===2){
                    this.firstType=rs.data.category.parentId
                }else{
                    this.first_type=rs.data.category.id
                }
                this.getCategoryList()
                this.getCategoryListInThis()
                this.cate_name=rs.data.category.name
            })
        },
        //删除
        deleteCateById(type,id)
        {
            if(confirm("确认删除吗？"))
            {
                axios.get('/api/deleteCateById',{
                    params:{
                        type,id
                    }
                }).then(rs=>{
                    this.infoFlag=true
                    if(rs.data.isDelete==="yes")
                    {
                        this.infoText="删除成功"
                        setTimeout(()=>{
                            window.location="/api/CategoryManage.html"
                        },1000)

                    }else{
                        this.infoText="删除失败,该类别下有商品或者子分类"
                    }
                })
            }

        },
        //修改
        updateCategory()
        {
            if(confirm("确认修改吗？"))
            {
                axios.get('/api/updateCategory',
                    {
                        params:{
                            name:this.cate_name,
                            parentId:this.firstType,
                            p_parentId:this.secondType,
                            type:this.category.type,
                            id:this.category.id
                        }
                    }).then(rs=>{
                        this.infoFlag=true
                        if(rs.data.isUpdate==="修改成功"){
                            this.infoText="修改成功"
                            setTimeout(()=>{
                                window.location="/api/CategoryManage.html"
                            },1000)
                        }else{
                            this.infoText="修改失败"
                        }
                })
            }
        },
        closeInfo()
        {
            this.infoFlag=false
        },
    },
    created()
    {
        this.getCategoryPage(1,5)
    }
})